<?php

class MMenu extends CWidget {

    /**
     * baseurl 
     * 
     * @var mixed
     * @access private
     */
    private $baseurl;
    /**
     * ulId The id of UL element for menu. 
     * 
     * @var string
     * @access protected
     */
    protected $ulId;
    /**
     * ulClasses The css classes for UL element. 
     * 
     * @var array
     * @access protected
     */
    protected $ulClasses;
    /**
     * cssFiles All css files needed to be included for this widget to run.
     * 
     * @var array
     * @access protected
     */
    protected $cssFiles;
    /**
     * jsFiles All js files needed to be included for this widge.
     * 
     * @var mixed
     * @access protected
     */
    protected $jsFiles;
    /**
     * menus 
     * 
     * @var array
     * @access protected
     */
    protected $menus;
    /**
     * Menu type.
     *
     * @var string
     */
    protected $type = 'dropdown-horizontal';

    /**
     * init Init the widget properties before run. 
     * 
     * @access public
     * @return void
     */
    public function init() {
        $this->ulId = 'nav';
        $this->ulClasses = array('dropdown', $this->type);
        $resource = dirname(__FILE__);
        $this->baseurl = Yii::app()->assetManager->publish($resource);
        $this->cssFiles = array(
            $this->baseurl . '/css/helper.css',
            $this->baseurl . '/css/dropdown.css',
            $this->baseurl . '/css/dropdown.vertical.css',
            $this->baseurl . '/css/default.ultimate.css',
        );
        $this->jsFiles = array(
            $this->baseurl . '/js/jquery/jquery.dropdown.js'
        );

        $this->registerClientScripts();
        parent::init();
    }

    /**
     * Setter for variable type.
     *
     * @param string $type
     */
    public function setType($type) {
        $this->type = $type;
    }

    /**
     * Getter for variable type.
     *
     * @return string
     */
    public function getType() {
        return $this->type;
    }

    /**
     * setMenus Setter for menus. 
     * 
     * @param array $menus 
     * @access public
     * @return void
     */
    public function setMenus(array $menus) {
        $this->menus = $menus;
    }

    /**
     * getMenus Getter method for menus. 
     * 
     * @access public
     * @return array Array contains menu items.
     */
    public function getMenus() {
        return $this->menus;
    }

    /**
     * setUlId Setter for id of UL element on the page. 
     * 
     * @param mixed $ulId 
     * @access public
     * @return void
     */
    public function setUlId($ulId) {
        $this->ulId = $ulId;
    }

    /**
     * setUlClasses Setter method for UL class on the page. 
     * 
     * @param array $ulClasses 
     * @access public
     * @return void
     */
    public function setUlClasses(array $ulClasses) {
        $this->ulClasses = $ulClasses;
    }

    /**
     * registerClientScripts 
     * 
     * @access public
     * @return void
     */
    public function registerClientScripts() {
        if (is_array($this->cssFiles) and count($this->cssFiles) > 0) {
            foreach ($this->cssFiles as $cssFile) {
                Yii::app()->clientScript->registerCssFile($cssFile);
            }
        }
        if (is_array($this->jsFiles) and count($this->jsFiles) > 0) {
            foreach ($this->jsFiles as $jsFile) {
                Yii::app()->clientScript->registerScriptFile($jsFile);
            }
        }
    }

    /**
     * run will be called with the widget is run. 
     * 
     * @access public
     * @return void
     */
    public function run() {
        $htmlMenu = $this->buildMenu();
        echo $htmlMenu;
    }

    private function checkMenu() {
        $valid = true;
        return $valid;
    }

    /**
     * buildMenu Build the menu. 
     * 
     * @access private
     * @return void
     */
    private function buildMenu() {
        $html = '';
        if (is_array($this->menus)) {
            $html = '<ul id="' . $this->ulId . '" class="' . implode(' ', $this->ulClasses) . '">';
            foreach ($this->menus as $menu) {
                if (!isset($menu['visible']) or $menu['visible'] == true) {
                    if (isset($menu['subMenus']) and count($menu['subMenus']) > 0) {
                        $html .= '<li class="topMenu"><span class="dir">' . $menu['label'] . '</span>';
                        $html .= '<ul>';
                        foreach ($menu['subMenus'] as $subMenu) {
                            if (!isset($subMenu['visible']) or $subMenu['visible'] == true) {
                                $targetFragm = '';
                                if (isset($subMenu['target']) and $subMenu['target'] != '') {
                                    $targetFragm = 'target="' . $subMenu['target'] . '"';
                                }
                                $html .= '<li><a ' . $targetFragm . ' href="' . $subMenu['url'][0] . '">' . $subMenu['label'] . '</a></li>';
                            }
                        }
                        $html .= '</ul>';
                        $html .= '</li>';
                    } else {
                        if (!isset($menu['visible']) or $menu['visible'] == true) {
                            $targetFragm = '';
                            if (isset($menu['target']) and $menu['target'] != '') {
                                $targetFragm = 'target="' . $menu['target'] . '"';
                            }
                            $html .= '<li class="topMenu"><a ' . $targetFragm . ' href="' . $menu['url'][0] . '">' . $menu['label'] . '</a></li>';
                        }
                    }
                }
            }
            $html .= '<script type="text/javascript">';
            $html .= '$(".topMenu").mouseover(
                        function() {
                        $(this).attr("style", "border-top: #FF0084 3px solid; margin-top:-3px");
                         }
                        );
                       $(".topMenu").mouseout(
                        function() {
                        $(this).removeAttr("style");
                         }
                        );
 
                      ';
            $html .= '</script>';
        } else {
            $html = 'Please specifiy an param named menus as array for this widget';
        }
        //$html1 = '
        //<ul id="nav" class="dropdown dropdown-horizontal">
        //<li><a href="./">Home</a></li>
        //<li><span class="dir">About Us</span>
        //<ul>
        //<li><a href="./">History</a></li>
        //<li><a href="./">Our Vision</a></li>
        //<li><span class="dir">The Team</span>
        //<ul>
        //<li><a href="./">Brigita</a></li>
        //<li><a href="./">John</a></li>
        //<li><a href="./">Michael</a></li>
        //<li><a href="./">Peter</a></li>
        //<li><a href="./">Sarah</a></li>
        //</ul>
        //</li>
        //<li><a href="./">Clients</a></li>
        //<li><a href="./">Testimonials</a></li>
        //<li><a href="./">Press</a></li>
        //<li><a href="./">FAQs</a></li>
        //<li class="divider"><a href="./">More...</a></li>
        //</ul>
        //</li>
        //<li><span class="dir">Services</span>
        //<ul>
        //<li><a href="./">Product Development</a></li>
        //<li><a href="./">Delivery</a></li>
        //<li><a href="./">Shop Online</a></li>
        //<li><a href="./">Support</a></li>
        //<li><a href="./">Training &amp; Consulting</a></li>
        //<li class="divider"><a href="./">More...</a></li>
        //</ul>
        //</li>
        //<li><span class="dir">Products</span>
        //<ul>
        //<li><a href="./" class="dir">New</a>
        //<ul>
        //<li><a href="./">Corporate Use</a></li>
        //<li><a href="./">Private Use</a></li>
        //</ul>
        //</li>
        //<li><a href="./" class="dir">Used</a>
        //<ul>
        //<li><a href="./">Corporate Use</a></li>
        //<li><a href="./">Private Use</a></li>
        //</ul>
        //</li>
        //<li><a href="./">Featured</a></li>
        //<li><a href="./">Top Rated</a></li>
        //<li><a href="./">Prices</a></li>
        //<li class="divider"><a href="./">More...</a></li>
        //</ul>
        //</li>
        //<li><a href="./">Gallery</a></li>
        //<li><a href="./">Events</a></li>
        //<li><a href="./">Careers</a></li>
        //<li><a href="./" class="dir">Contact Us</a>
        //<ul>
        //<li><a href="./">Enquiry Form</a></li>
        //<li><a href="./">Map &amp; Driving Directions</a></li>
        //<li><a href="./">Your Feedback</a></li>
        //<li class="divider"><a href="./">More...</a></li>
        //</ul>
        //</li>
        //</ul>';
        return $html;
    }

}
